<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$return=array();
$return['item']=$this->plugin_setting('setting');
$setting=core_setting('cloud');//获取云服务参数
//生成前端签名
$sign=sha1(TIMESTAMP.$setting['cloudkey'].$_SESSION['uniacid'].$_SESSION['uid'].$setting['secret']);
//拼接前端发送消息的前缀
$return['message_pre1']=$sign.';'.TIMESTAMP.';'.$setting['cloudkey'].';'.$_SESSION['uniacid'].';'.$_SESSION['uid'].';';
$set=$this->plugin_setting('setting');
if(!$set['black']){
	$set['black']=5;
}
if(!$set['time']){
	$set['time']=24;
}
if($_RQ['op']=='main'){
	$info=pdo_get('jy_taxi_driver',array('mid'=>$_SESSION['uid']));
	if($info['busy']==2){
		exi('您还有未处理的订单，不可以接新订单！','error','jy_taxi/order/');
	}
	if($info['notimes']>=$set['black']){
		if(($info['update_time']+3600*$set['time'])>time()){
			exi('您拒单超过五次，冻结24小时，暂时不能接单','error');
		}else{
			pdo_update('jy_taxi_driver',array('stop'=>0,'notimes'=>0),array('mid'=>$_SESSION['uid']));
		}
	}
	pdo_update('jy_taxi_driver',array('busy'=>1),array('mid'=>$_SESSION['uid']));
	$return['info']=array('show'=>0);
	exi($return);
}
if($_RQ['op']=='ok'){
	$post=get_postdata();
	$data=array_fields(array('price','jjuli','price2','lat','lat2','order'),$post);	
	$order=pdo_get('jy_taxi_order',array('id'=>intval($post['orderid'])));
	$mid=$order['mid'];
	$driver=pdo_get('jy_taxi_driver',array('mid'=>$_SESSION['uid']));
	//$jjuli=cfc('tools')->getdistance($post['lat']['lng'],$post['lat']['lat'],$post['lat2']['lng'],$post['lat2']['lat']);
	//$jjuli=intval($jjuli/1000);
	pdo_update('jy_taxi_order',array('driver_id'=>$driver['id'],'jjuli'=>$data['jjuli'],'price2'=>$data['price2'],'price'=>$data['price'],'status'=>1,'driver_id'=>$driver['id']),array('id'=>$post['orderid']));
	
	$m=array('name'=>$driver['name'],'time'=>$post['time'],'juli'=>$post['juli'],'mobile'=>$driver['mobile'],'price'=>$data['price'],'price1'=>$order['price1'],'jjuli'=>$data['jjuli'],'price2'=>$data['price2']);
	$message=ijson_encode($m);
	cfc('jinyun_socket')->send2uid($message,$mid);
	//$post2=array(
	//	'message'=>$return['message_pre1'].$mid.';'.$message,
	//);
	//cloud_request('cloud.forward',$post2);
	if($_SESSION['os']=='wxapp'){
		exi('已接单');
	}else{
		exi('已接单','','jy_taxi/daohang/main/orderid='.$post['orderid']);
	}
}
if($_RQ['op']=='no'){
	$info=pdo_get('jy_taxi_driver',array('mid'=>$_SESSION['uid']));
	$post=get_postdata();
	$orderid=$post['order'];
	$order=pdo_get('jy_taxi_order',array('id'=>$orderid));
	if($order['nolist']){
		$nolist=json_decode($order['nolist'],true);
		$nolist[]=$_SESSION['uid'];
	}else{
		$nolist=array($_SESSION['uid']);
	}
	$no=json_encode($nolist);
	
	$drivers=pdo_getall('jy_taxi_driver',array('uniacid'=>$_SESSION['uniacid'],'busy'=>0,'status'=>1,'stop'=>0));	
	pdo_update('jy_taxi_order',array('nolist'=>$no),array('id'=>$orderid));
	if(!$drivers){
		pdo_update('jy_taxi_order',array('status'=>6),array('id'=>$orderid));
		if($info['notimes']>=($set['black']-1)){
			pdo_update('jy_taxi_driver',array('busy'=>0,'notimes +'=>1,'stop'=>1),array('mid'=>$_SESSION['uid']));	
			
		}else{
			pdo_update('jy_taxi_driver',array('busy'=>0,'notimes +'=>1),array('mid'=>$_SESSION['uid']));
			
		}
	exi('已拒绝','','/index/');	
		
	}
	
	foreach($drivers as $k=>&$v){		
		$v['latlng']=json_decode($v['latlng'],true);
		$v['juli']=cfc('tools')->getdistance($post['lat']['lng'],$post['lat']['lat'],$v['latlng']['lng'],$v['latlng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		if(in_array($v['mid'],$nolist)){
			unset($drivers[$k]);
		}
	}
	paixu($drivers,'juli');	
	
	if(intval($return['item']['jkjuli']) < $drivers['0']['juli']){
		$price2=($drivers['0']['juli']-$return['item']['jkjuli'])*$return['item']['price2'];
	}else{
		$price2=0;
	}
	$price=$order['price1']+$price2;
	$sign=sha1(TIMESTAMP.$setting['cloudkey'].$_SESSION['uniacid'].$order['mid'].$setting['secret']);
	//拼接前端发送消息的前缀
	$message_pre1=$sign.';'.TIMESTAMP.';'.$setting['cloudkey'].';'.$_SESSION['uniacid'].';'.$order['mid'].';';
	$m=array('lat'=>json_decode($order['lat'],true),'orderid'=>$orderid,'name'=>$order['name'],'mobile'=>$order['mobile'],'chufa'=>$order['start_seat'],'mudi'=>$order['dest_seat'],'jjuli'=>$drivers['0']['juli'],'juli'=>$order['juli'],'price'=>$price,'price2'=>$price2);
	$message=ijson_encode($m);
	cfc('jinyun_socket')->send2uid($message,$drivers['0']['mid']);
	//$post=array(
	//	'message'=>$message_pre1.$drivers['0']['mid'].';'.$message,
	//);
	//cloud_request('cloud.forward',$post);
	if($info['notimes']>=($set['black']-1)){
			pdo_update('jy_taxi_driver',array('busy'=>0,'notimes +'=>1,'stop'=>1),array('mid'=>$_SESSION['uid']));	
			
		}else{
			pdo_update('jy_taxi_driver',array('busy'=>0,'notimes +'=>1),array('mid'=>$_SESSION['uid']));
			
		}
	exi('已拒绝','','/index/');
}
if($_RQ['op']=='dw'){
	$post=get_postdata();
	pdo_update('jy_taxi_driver',array('latlng'=>json_encode($post['latlng'])),array('mid'=>$_SESSION['uid']));
	exi($return);
}
?>